package com.example.myblog01.web.admin;


import com.example.myblog01.pojo.Blog;
import com.example.myblog01.pojo.User;
import com.example.myblog01.service.BlogService;
import com.example.myblog01.service.TagService;
import com.example.myblog01.service.TypeService;
import com.example.myblog01.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;


//两个size要相同  不然数据页面会出现bug
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        Model model, BlogQuery blog) {
//        查询type的所有分类 存进types
        model.addAttribute("types", typeService.listType());
//        updateTime按更新时间来排序
        model.addAttribute("page", blogService.listBlog(pageable, blog));

        return "admin/blogs";
    }


    @PostMapping("/blogs/search")
    public String secrch(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         Model model, BlogQuery blog) {
//        updateTime按更新时间来排序

        model.addAttribute("page", blogService.listBlog(pageable, blog));

//        使用thymeleaf  只返回部分片段  不刷新页面
        return "admin/blogs :: blogList";
    }

//    新增
    @GetMapping("/blogs/input")
    public String input(Model model) {
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }

    @PostMapping("/blogs")
    public String post(Blog blog, HttpSession session, RedirectAttributes attributes){
        blog.setUser((User) session.getAttribute("user"));
//        从前端拿到选择的type
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));

        Blog t = blogService.seveblog(blog);
        if (t == null) {
            attributes.addFlashAttribute("message", "添加失败！");
        } else {
            attributes.addFlashAttribute("message", "添加成功！");
        }

        return "redirect:/admin/blogs";
    }


//    编辑
    @GetMapping("/blogs/{id}/input")
    public String editinput(Model model,@PathVariable Long id) {
//        从后台获取并装入types tags
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
//d对tag处理
        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog", blog);
        return "admin/blogs-input";
    }


    @GetMapping("/blogs/{id}/delete")
    public String deleteblog(@PathVariable Long id,RedirectAttributes attributes){
        System.out.println("-------------------------sssss");
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功！");
        return "redirect:/admin/blogs";

    }


}
